package com.weirong.smartvault.backservice.entity_model;

import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.CommandRepository;
import com.weirong.smartvault.dao.DepartmentRepository;
import com.weirong.smartvault.dao.ForeignCurrencyRequestDetailRepository;
import com.weirong.smartvault.entity.ForeignCurrencyRequestDetail;
import com.weirong.smartvault.backservice.user.IUser;
import com.weirong.smartvault.entity.Department;
import com.weirong.smartvault.entity.ForeignCurrencyRequest;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 于2017/12/11由 KFEB4 创建 .
 */
public class ForeignCurrencyRequestModel implements ListModelInterface {
    ForeignCurrencyRequest foreignCurrencyRequest;
    List<String> keys;
    List<String> keysNames;
    DepartmentRepository departmentRepository;
    ForeignCurrencyRequestDetailRepository foreignCurrencyRequestDetailRepository;
    CommandRepository commandRepository = SpringContextUtil.getBean(CommandRepository.class);

    public ForeignCurrencyRequestModel(ForeignCurrencyRequest foreignCurrencyRequest){
        this.foreignCurrencyRequest = foreignCurrencyRequest;
        keys = new ArrayList<>();
        keysNames = new ArrayList<>();
        foreignCurrencyRequestDetailRepository = SpringContextUtil.getBean(ForeignCurrencyRequestDetailRepository.class);
        departmentRepository = SpringContextUtil.getBean(DepartmentRepository.class);
        keysNames.add("需求时间");
        keys.add("getRequestDate");
        keysNames.add("需求网点");
        keys.add("getDepartmentName");
        keysNames.add("申请人");
        keys.add("getHandleName");
    }
    public String getHandleName(){
        IUser user = IUser.findOne(foreignCurrencyRequest.getHandler());
        return user.getUserName();
    }
    public String getRequestDate(){
        return DateUtil.getNomalString(foreignCurrencyRequest.getRequestDate());
    }
    public String getDepartmentName(){
        Integer id = foreignCurrencyRequest.getBranchId();
        Department department= departmentRepository.findOne(foreignCurrencyRequest.getBranchId());
        return department.getName();
    }
    public Integer getId(){
        return foreignCurrencyRequest.getId();
    }

    @Override
    public List<String> keys() {
        return keys;
    }

    @Override
    public List<String> keyNames() {
        return keysNames;
    }

    public List<MoneyLogModel> getAllDetailLogs() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        List<ForeignCurrencyRequestDetail> foreignCurrencyRequestDetails = foreignCurrencyRequestDetailRepository.findAllByRequestId(foreignCurrencyRequest.getId());
        return ListTool.cast(foreignCurrencyRequestDetails,MoneyLogModel.class);
    }

    public List<CommandModel> getAllCommands() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        return ListTool.cast(commandRepository.findAllByRequestId(getId()),CommandModel.class);
    }

    public Collection<MoneyLogCollectionModel<MoneyLogModel>> toMoneyLogCollection() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
        return MoneyLogCollectionModel.makeCollection(getAllDetailLogs());
    }
}
